Skip to content

Conversation

@chmjkb
Copy link
Contributor

@chmjkb chmjkb commented Jan 13, 2026

Description

Previously, errors were thrown as plain strings or unstructured exceptions, making it impossible for users to programmatically handle different error types. This PR introduces type-safe, structured error handling across the entire codebase.

Changes

1. Codegen Approach

  • Added single source of truth for error codes in scripts/errors.config.ts
  • Generates matching C++ (ErrorCodes.h) and TypeScript (ErrorCodes.ts) enums
  • Preserves JSDoc comments from config to generated files
  • Run yarn codegen:errors to regenerate after modifying error definitions

2. Error Structure

All errors now follow a consistent format: {code: number, message: string}

C++ side:

  • Throw RnExecutorchError(ErrorCode, "message") instead of std::runtime_error
  • Use RnExecutorchInternalError::ErrorName for internal errors
  • ExecuTorch runtime errors are passed directly via the variant
  • Updated all model files: BaseModel.cpp, LLM.cpp, Detector.cpp, Recognizer.cpp, VerticalOCR.cpp, etc.

TypeScript side:

  • Throw ExecutorchError(ETErrorCode.ErrorName, "message") instead of plain Error
  • Use parseUnknownError(err) in catch blocks to convert unknown errors
  • Updated all modules, controllers, and hooks

3. Error Code Ranges

Currently that's used pretty frivolously and this needs to be fixed.

4. Key Quirk: Different Enums for C++ vs TypeScript

  • C++ enum (RnExecutorchInternalError): Only includes internal errors, excludes ExecuTorch runtime errors
  • TypeScript enum (ETErrorCode): Includes all errors (internal + ExecuTorch mapped)
  • Reason: C++ handles ExecuTorch runtime errors via ErrorVariant = std::variant<RnExecutorchInternalError, executorch::runtime::Error>

5. Documentation

Added error handling documentation at docs/docs/03-typescript-api/04-error-handling.md:

  • Overview with example code
  • Complete reference of all error codes organized by category
  • "When It Occurs" and "How to Handle" guidance for each error
  • Best practices for retry logic, input validation, and state management

Important: From Now On

When throwing errors from the native side, always use RnExecutorchError with appropriate error codes from RnExecutorchInternalError enum. This ensures errors are properly serialized across the JSI boundary and can be handled by users.

Introduces a breaking change?

  • Yes
  • No

Type of change

  • Bug fix (change which fixes an issue)
  • New feature (change which adds functionality)
  • Documentation update (improves or adds clarity to existing documentation)
  • Other (chores, tests, code style improvements etc.)

Tested on

  • iOS
  • Android

Testing instructions

Screenshots

Related issues

Checklist

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional notes

@msluszniak msluszniak added the feature PRs that implement a new feature label Jan 13, 2026
@msluszniak msluszniak linked an issue Jan 13, 2026 that may be closed by this pull request
@chmjkb chmjkb marked this pull request as ready for review January 13, 2026 15:52
@chmjkb chmjkb requested review from IgorSwat and benITo47 January 13, 2026 15:52
Copy link
Member

@msluszniak msluszniak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any testing for this one other than sanity manual testing?

@chmjkb
Copy link
Contributor Author

chmjkb commented Jan 14, 2026

Do we have any testing for this one other than sanity manual testing?

Not yet 👎🏻

@chmjkb chmjkb force-pushed the @chmjkb/improve-error-handling branch from 34d01b3 to dcce471 Compare January 16, 2026 14:14
@benITo47
Copy link
Contributor

Because you've been rebasing, some files are still using std::runtime_error (DetectorUtils.cpp, Kokoro.cpp, etc.) please, try to cover those cases as well.

Same thing applies for Typescript - useTextToSpeech.ts, TextToSpeechModule.ts, OCRController.ts ... - are throwing "new Error" instead of RnExecutorchError.

@chmjkb chmjkb requested a review from benITo47 January 16, 2026 18:52
@msluszniak
Copy link
Member

I think we need to do something with this iOS CI since now majority of jobs fails with this strange error. And in fact it is not deterministic, sometimes it works perfectly fine.

Copy link
Contributor

@benITo47 benITo47 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature PRs that implement a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve error handling after C++ port

4 participants